<template>
  <view class="container">
    <!-- 自定义导航栏 -->
    <u-navbar
      :is-back="false"
      title="图书馆预约系统"
      title-color="#ffffff"
      :background="{ background: 'linear-gradient(to right, #1A8DFF, #0269D3)' }"
    ></u-navbar>
    
    <!-- 轮播图 -->
    <view class="carousel-section">
      <u-swiper
        :list="carouselList"
        keyName="image_url"
        :height="220"
        :border-radius="10"
        mode="aspectFill"
        indicator-pos="bottomRight"
        @click="clickSwiper"
      ></u-swiper>
    </view>
    
    <!-- 快速预约 -->
    <view class="quick-reserve">
      <view class="section-title">
        <text>快速预约</text>
      </view>
      <view class="quick-buttons">
        <view class="quick-button" @click="goToReserve">
          <u-icon name="calendar" size="30" color="#1A8DFF"></u-icon>
          <text>预约座位</text>
        </view>
        <view class="quick-button" @click="goToRecords">
          <u-icon name="file-text" size="30" color="#1A8DFF"></u-icon>
          <text>我的预约</text>
        </view>
      </view>
    </view>
    
    <!-- 资讯列表 -->
    <view class="news-section">
      <view class="section-title">
        <text>最新资讯</text>
        <text class="more" @click="goToNewsList">更多</text>
      </view>
      <view class="news-list">
        <view 
          class="news-item" 
          v-for="(item, index) in newsList" 
          :key="index"
          @click="goToNewsDetail(item.id)"
        >
          <image class="news-image" :src="item.cover_image" mode="aspectFill"></image>
          <view class="news-content">
            <text class="news-title">{{ item.title }}</text>
            <text class="news-date">{{ item.create_time }}</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      carouselList: [],
      newsList: []
    }
  },
  computed: {
    ...mapGetters(['userInfo', 'hasLogin'])
  },
  onLoad() {
    this.getCarousels()
    this.getNewsList()
  },
  onShow() {
    // 检查登录状态
    if (!this.hasLogin) {
      this.checkLogin()
    }
  },
  methods: {
    // 获取轮播图
    async getCarousels() {
      try {
        const res = await this.$api.getCarousels()
        if (res.data && res.data.code === 200) {
          this.carouselList = res.data.data || []
        }
      } catch (error) {
        console.error('获取轮播图失败', error)
      }
    },
    
    // 获取资讯列表
    async getNewsList() {
      try {
        const res = await this.$api.getNewsList({
          page: 1,
          size: 3
        })
        if (res.data && res.data.code === 200) {
          this.newsList = res.data.data.rows || []
        }
      } catch (error) {
        console.error('获取资讯列表失败', error)
      }
    },
    
    // 点击轮播图
    clickSwiper(index) {
      const item = this.carouselList[index]
      if (item.link_url) {
        // 如果有链接则跳转
        uni.navigateTo({
          url: item.link_url
        })
      }
    },
    
    // 跳转到预约页面
    goToReserve() {
      if (!this.checkLogin()) return
      
      uni.switchTab({
        url: '/pages/reserve/index'
      })
    },
    
    // 跳转到预约记录页面
    goToRecords() {
      if (!this.checkLogin()) return
      
      uni.switchTab({
        url: '/pages/record/index'
      })
    },
    
    // 跳转到资讯列表
    goToNewsList() {
      uni.navigateTo({
        url: '/pages/news/index'
      })
    },
    
    // 跳转到资讯详情
    goToNewsDetail(id) {
      uni.navigateTo({
        url: `/pages/news/detail?id=${id}`
      })
    },
    
    // 检查登录状态
    checkLogin() {
      if (!this.hasLogin) {
        uni.showModal({
          title: '提示',
          content: '请先登录',
          confirmText: '去登录',
          success: (res) => {
            if (res.confirm) {
              uni.navigateTo({
                url: '/pages/login/login'
              })
            }
          }
        })
        return false
      }
      return true
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding-bottom: 30rpx;
}

.carousel-section {
  padding: 20rpx;
}

.section-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx;
  font-size: 32rpx;
  font-weight: bold;
  
  .more {
    font-size: 24rpx;
    color: #1A8DFF;
    font-weight: normal;
  }
}

.quick-reserve {
  margin: 20rpx;
  background-color: #ffffff;
  border-radius: 10rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  padding-bottom: 20rpx;
}

.quick-buttons {
  display: flex;
  justify-content: space-around;
  padding: 20rpx;
}

.quick-button {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20rpx 40rpx;
  
  text {
    margin-top: 10rpx;
    font-size: 28rpx;
  }
}

.news-section {
  margin: 20rpx;
  background-color: #ffffff;
  border-radius: 10rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  padding-bottom: 20rpx;
}

.news-list {
  padding: 0 20rpx;
}

.news-item {
  display: flex;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #f5f5f5;
  
  &:last-child {
    border-bottom: none;
  }
}

.news-image {
  width: 180rpx;
  height: 120rpx;
  border-radius: 8rpx;
  margin-right: 20rpx;
}

.news-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.news-title {
  font-size: 28rpx;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

.news-date {
  font-size: 24rpx;
  color: #999;
}
</style>
